package common.util.emailUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : shibixiao
 * @description : 邮件模板工具类
 * @date : 2021-06-30 11:51
 **/
public class TemplateUtils {

    /**
     * 占位符前缀
     */
    private static final String PLACEHOLDER_PREFIX = "${";
    /**
     * 占位符后缀
     */
    private static final String PLACEHOLDER_SUFFIX = "}";

    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("aaa","小名");
        map.put("bbb","晴天");
        map.put("ccc",null);

        System.out.println(doParseMailTemplate("我是今天的${aaa}，今天天气是${bbb}，最高气温${ccc}。",map));
    }

    public static String doParseMailTemplate(String mailTemplate, Map<String, Object> parameters) {
        if (mailTemplate == null || mailTemplate.isEmpty()) {
            return "";
        }
        int start = mailTemplate.indexOf(PLACEHOLDER_PREFIX);
        if (start == -1) {
            return mailTemplate;
        }
        char[] src = mailTemplate.toCharArray();
        int offset = 0;
        final StringBuilder builder = new StringBuilder();
        StringBuilder paramExpression = null;
        while (start > -1) {
            if (start > 0 && src[start - 1] == '\\') {
                builder.append(src, offset, start - offset - 1).append(PLACEHOLDER_PREFIX);
                offset = start + PLACEHOLDER_PREFIX.length();
            } else {
                if (paramExpression == null) {
                    paramExpression = new StringBuilder();
                } else {
                    paramExpression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + PLACEHOLDER_PREFIX.length();
                int end = mailTemplate.indexOf(PLACEHOLDER_SUFFIX, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        paramExpression.append(src, offset, end - offset - 1).append(PLACEHOLDER_SUFFIX);
                        offset = end + PLACEHOLDER_SUFFIX.length();
                        end = mailTemplate.indexOf(PLACEHOLDER_SUFFIX, offset);
                    } else {
                        paramExpression.append(src, offset, end - offset);
                        break;
                    }
                }
                if (end == -1) {
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    if(Objects.isNull(parameters.get(paramExpression.toString()))) {
                        builder.append(PLACEHOLDER_PREFIX+paramExpression.toString()+PLACEHOLDER_SUFFIX);
                    }else {
                        builder.append(parameters.get(paramExpression.toString()));
                    }
                    offset = end + PLACEHOLDER_SUFFIX.length();
                }
            }
            start = mailTemplate.indexOf(PLACEHOLDER_PREFIX, offset);
        }
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }
}
